package com.lp.juc;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class FutureTest {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        Runnable r1 = new Runnable() {
            @Override
            public void run() {
                try{
                    Thread.sleep(1000);
                    System.out.println("runnable1准备完毕！");
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        };
        Runnable r2 = new Runnable() {
            @Override
            public void run() {
                try{
                    Thread.sleep(3000);
                    System.out.println("runnable2准备完毕！");
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        };

        Callable c1 = new Callable() {
            @Override
            public Object call() throws Exception {
                try {
                    Thread.sleep(1000);
                }catch (Exception e){
                    e.printStackTrace();
                }
                return "callable1准备完毕！";
            }
        };

        Callable c2 = new Callable() {
            @Override
            public Object call() throws Exception {
                try {
                    Thread.sleep(3000);
                }catch (Exception e){
                    e.printStackTrace();
                }
                return "callable2准备完毕！";
            }
        };
//        long start1 = System.currentTimeMillis();
//        Thread t1 = new Thread(r1);
//        t1.start();
//        t1.join();
//
//        Thread t2 = new Thread(r2);
//        t2.start();
//        t2.join();
//        long end1 = System.currentTimeMillis();
//        System.out.println("runnable准备时间:"+(end1-start1));
        long start2 = System.currentTimeMillis();
        FutureTask<String> ft1 = new FutureTask<>(c1);
        Thread t3 = new Thread(ft1);
        t3.start();
        FutureTask<String> ft2 = new FutureTask<>(c2);
        Thread t4 = new Thread(ft2);
        t4.start();
        int count = 0;
        while(true){
            if(ft1.isDone() && ft2.isDone()){
                break;
            }else{
                if(!ft1.isDone()){
                    System.out.println("callable1正在准备！");
                }else{
                    System.out.println(ft1.get());
                }
                if(!ft2.isDone()){
                    System.out.println("callable2正在准备！");
                    if(count > 1){
                        ft2.cancel(true);
                        System.out.println("callable2取消准备！");
                    }
                }else{
                    System.out.println(ft2.get());
                }
            }
            Thread.sleep(1000);
            count++;
        }

        long end2 = System.currentTimeMillis();
        System.out.println("callable准备时间:"+(end2-start2));
    }


}
